// pages/Topromote/Topromote.js
// 获取下单可选择的优惠券   我的优惠券
import { getCreateList, getMyTopromeList } from '../../config/https'
const tool = require("../../config/tool");

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadMore: false,    //加载中提示
    finished: false,    //是否已经请求完全部数据
    indexs:'',     //判断优惠券是否可用 0未使用 1已使用 2已过期
    page:0,      //页数
    list: [],    //列表数据
    type: '',    //判断是否是下单页面跳转过来的1是
  },



  onChange(event) {
    console.log(event)
    this.setData({
      page:0,
      list:[],
      indexs:event.detail.index
    })
    this.pullUpData();
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //判断是否是下单页面过来的如果是为1
    if (options.type) {
      that.setData({
        type: options.type
      })
      //读取其他页面过来的id
      var id = JSON.parse(options.id)
      if (!options.optionid) {
        var optionid = [''];
        optionid.length = id.length
      } else {
        var optionid = JSON.parse(options.optionid)
      }
      var total = JSON.parse(options.total)
      var goods_data = new Object();
      for (let i = 0; i < id.length; i++) {
        goods_data[i] = {
          'id': id[i],
          'optionid': optionid[i],
          'total': total[i]
        }
      }
      console.log(goods_data)
      getCreateList({
        goods: goods_data
      }).then(res=>{
        console.log(res)
        var list = res.coupons
        //开始时间
        list.forEach(item => {
          item.timestart = tool.u_formatTimestamp(
            item.timestart * 1000,
            {
              type: "ymd"
            }
          );
          item.timeend = tool.u_formatTimestamp(
            item.timeend * 1000,
            {
              type: "ymd"
            }
          );
        })
        that.setData({
          list
        })
      })
    }else{
      this.setData({
        page:0
      })
      this.pullUpData()
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 上拉加载数据
  pullUpData(){
    var that = this
    let cate = ''
    that.setData({ loadMore: true })
    if(that.data.indexs==0){

    } else if (that.data.indexs == 1){
      cate = 'used'
    }else{
      cate ='past'
    }
    getMyTopromeList({
      page: ++that.data.page,
      cate: cate
    }).then(res=>{
      console.log(res)
      that.setData({ loadMore: false })
      let list = res[0].list
      //开始时间
      list.forEach(item => {
        item.timestart = tool.u_formatTimestamp(
          item.timestart * 1000,
          {
            type: "ymd"
          }
        );
        item.timeend = tool.u_formatTimestamp(
          item.timeend * 1000,
          {
            type: "ymd"
          }
        );
      })
      list = list.concat(that.data.list)
      if (list.length == 0) {
      } else {
        that.setData({
          finished: res[0].list.length < res[0].pagesize ? true : false
        })
      }
      wx.stopPullDownRefresh();
      that.setData({
        list
      })
    })
  },
  // 点击返回下单页面
  onGuBack(e){
    var { id, deduct, couponname} = e.currentTarget.dataset
    let toprom = JSON.stringify({
      id: id,
      deduct: deduct,
      couponname: couponname
    })
    if(this.data.type=='1'){
      wx.setStorage({
        key: 'toprom',
        data: toprom,
        success() {
          wx.navigateBack({
          })
        },
      })
    }
  },
  onIndex(){
    wx.reLaunch({
      url: '/pages/tapswitching/tapswitching',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.finished && !this.data.loadMore) {
      this.pullUpData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})